package us.versus.them.weeds.constraints;

import flash.display.Graphics;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.Lib;

import us.versus.them.weeds.Vertlet;

class Box implements Constraint {
	var vertlets:Array< Vertlet >;
	var width:Int;
	var height:Int;

	public static var X_Y_OK      = 0;
	public static var X_TOO_SMALL = 1;
	public static var X_TOO_BIG   = 2;
	public static var Y_TOO_SMALL = 4;
	public static var Y_TOO_BIG   = 8;

	public var bounds : Int;

	public function new( vertlets:Array< Vertlet >, width:Int, height:Int ) {
		this.vertlets = vertlets;
		this.width = width;
		this.height = height;
	}

	public function enforce() {
		this.bounds = X_Y_OK;
		for ( vertlet in this.vertlets ) {
			this.bounds |= this.enforce1( vertlet );
		}
	}

	public function enforce1( vertlet:Vertlet ) {
		var bounds = X_Y_OK;
		if ( vertlet.current.x < 0 ) {
			bounds |= X_TOO_SMALL;
			vertlet.current.x = 0;
		}
		if ( vertlet.current.y < 0 ) {
			bounds |= Y_TOO_SMALL;
			vertlet.current.y = 0;
		}
		if ( vertlet.current.x > width ) {
			bounds |= X_TOO_BIG;
			vertlet.current.x = width;
		}
		if ( vertlet.current.y > height  ) {
			bounds |= Y_TOO_BIG;
			vertlet.current.y = height;
		}
		return bounds;
	}
}
